package ird.demo.controller.transaction;

import ird.demo.bo.AjaxResult;
import ird.demo.common.DemoConstant;
import ird.demo.common.SysUtil;
import ird.demo.domain.TranGrp;
import ird.demo.domain.Transaction;
import ird.demo.service.TranGrpService;
import ird.demo.service.TranService;
import ird.demo.vo.IRSM0003.IRSM0003TranVo;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/transaction/IRSM0003")
public class IRSM0003Controller extends TranController {
	
	@Autowired
	private TranService tranSvc;
	
	@Autowired
	private TranGrpService tranGrpSvc;
	
	@Override
	protected String getTc() {
		return "IRSM0003";
	}

	@Override
	protected void preInput(ModelMap model) {
	}
	
	@Override
	protected void preDetail(HttpServletRequest request, HttpSession session, ModelMap model) {
		String act = request.getParameter("act");
		System.out.println("act="+act);
		model.addAttribute("act", act);
		String actDesc = null;
		
		Map<String,String> keyParam = new HashMap<String,String>();
		if("add".equals(act)){
			actDesc = "Add Transaction Group";
			keyParam.put("act", "add");
			model.addAttribute("actDesc", actDesc);
		}
		else if ("upd".equals(act)){
			actDesc = "Modify Transaction Group";
			
			String tranGrp = request.getParameter("tranGrp");
			String rowIdx = request.getParameter("rowIdx");
			String page = request.getParameter("page");
			String rows = request.getParameter("rows");
			
			keyParam.put("act", "upd");
			keyParam.put("tranGrp", tranGrp);
			keyParam.put("rowIdx", rowIdx);
			keyParam.put("page", page);
			keyParam.put("rows", rows);
			session.setAttribute(DemoConstant.KEY_PARAM, keyParam);
			
			System.out.println("tranGrp"+tranGrp);
			System.out.println("rowIdx"+rowIdx);
			System.out.println("page"+page);
			System.out.println("rows"+rows);
			System.out.println("============tranGrp="+tranGrp);
			TranGrp tranGrpBean = tranGrpSvc.findTranGrpById(tranGrp);
			System.out.println("============tranGrpBean="+tranGrpBean);
			
			List<IRSM0003TranVo> tranVoList =  getTranVoList(tranGrp);
			model.addAttribute("actDesc", actDesc);
			model.addAttribute("tranGrp", tranGrpBean);
			model.addAttribute("tranVoList", tranVoList);
		}
		//'del' will delete directly then jump to done screen(skip detail screen)
		else if ("del".equals(act)){
			System.out.println("tranGrps"+request.getParameter("tranGrps"));
			String[] tranGrpArr = request.getParameter("tranGrps").split(",");
			for(String tranGrp : tranGrpArr){
				System.out.println("delete tranGrp["+tranGrp+"]");
				tranGrpSvc.delTranGrp(tranGrp);
			}
			String redirectUrl = done(request, session, model);
			model.addAttribute(DemoConstant.REDIRECT_UTL, redirectUrl);
		}
		
		session.setAttribute(DemoConstant.KEY_PARAM, keyParam);
	}
	
	@RequestMapping(value="/validAndUpd/{act}", method = RequestMethod.POST)
	@ResponseBody
	public String validAndUpd(@RequestBody TranGrp tranGrp, @PathVariable("act") String act, Principal principal){
//	public String validAndUpd(@RequestBody Map paraMap,HttpServletRequest request, @PathVariable("act") String act, Principal principal){
		System.out.println("==========validAndUpd");
		
		AjaxResult result = null;
		System.out.println("act="+act);
		System.out.println("tranGrp="+tranGrp);
		
		
//		System.out.println("paraMap="+paraMap);
//		System.out.println("request paraMap="+request.getParameterMap());
//		System.out.println("tranGrp="+request.getParameter("tranGrp"));
//		System.out.println("tranGrpDesc="+request.getParameter("tranGrpDesc"));
//		System.out.println("tranId="+request.getParameter("tranId"));
//		System.out.println("tranIds="+request.getParameterValues("tranId"));
//		result = new AjaxResult(DemoConstant.SUCCESS, null, null);
		result = new AjaxResult(DemoConstant.SUCCESS, null, null);
		return gson.toJson(result);
	}
	
	@RequestMapping("/getTranGrpListPage")
	@ResponseBody
	public String getTranGrpListPage(
			@RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer rows,
            @RequestParam(required = false) String sort,
            @RequestParam(required = false) String order,
            Model model){
		System.out.println("BaseController.list, page="+page+",rows="+rows+",sort="+sort+",order="+order);
		int pageNum = page==null?1:page-1;
		int rowNum = rows==null?DemoConstant.PAGE_SIZE:rows;
		String sortStr = sort==null?"tranGrp":sort;
		Direction orderDirect = DemoConstant.DESC.equalsIgnoreCase(order)?Direction.DESC:Direction.ASC;
		
		PageRequest pgReq = new PageRequest(pageNum, rowNum, orderDirect, sortStr);
		System.out.println("request="+gson.toJson(pgReq));
		Page<TranGrp> tranPage = tranGrpSvc.findPageTranGrp(pgReq);
		model.addAttribute(DemoConstant.ROWS, tranPage.getContent());
		model.addAttribute(DemoConstant.TOTAL, tranPage.getTotalElements());
		return gson.toJson(model);
	}
	
	//for display all transaction IRSM0003 detail page
	//if a transaction under tranGrp, set isCheck=true
	private List<IRSM0003TranVo> getTranVoList(String tranGrp){
		List<Transaction> allTrans = tranSvc.findAllTran();
		
		List<Transaction> tranListInTranGrp = null;
		if(SysUtil.isNullstr(tranGrp) == false){
			tranListInTranGrp = tranSvc.findTranByTranGrp(tranGrp);
		}
		
		List<IRSM0003TranVo> tranVoList = new ArrayList<IRSM0003TranVo>();
		for(Transaction tran : allTrans){
			IRSM0003TranVo tranVo = new IRSM0003TranVo();
			tranVo.setTranId(tran.getTranId());
			tranVo.setTranDesc(tran.getTranDesc());
			if(tranListInTranGrp != null){
				for(Transaction tranInGrp : tranListInTranGrp){
					if(tranVo.getTranId().equals(tranInGrp.getTranId())){
						tranVo.setChecked("checked");
						System.out.println("check true for:"+tranVo.getTranId());
						break;
					}
				}
			}
			tranVoList.add(tranVo);
		}
		
		return tranVoList;
	}
	
}
